package com.tangcco.lockwell.service.impl;

import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tangcco.lockwell.context.WebContext;
import com.tangcco.lockwell.domain.Message;
import com.tangcco.lockwell.repository.MessageMapper;
import com.tangcco.lockwell.service.MessageService;
import com.tangcco.lockwell.utility.AiUtil;
import com.tangcco.lockwell.utility.DateTimeUtil;
import com.tangcco.lockwell.viewmodel.admin.message.MessagePageRequestVM;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
@Slf4j
public class MessageServiceImpl implements MessageService {
    @Resource
    MessageMapper messageMapper;
    @Resource
    private AiUtil aiUtil;
    @Resource
    private WebContext webContext;
    @Override
    public List<Message> selectlikes(String userName) {
        return messageMapper.selectlikes(userName);
    }

    @Override
    public int deletelikes(Integer messageId) {
        return messageMapper.deletelikes(messageId);
    }

    @Override
    public PageInfo<Message> selectPage(MessagePageRequestVM messagePageRequestVM) {
        PageHelper.startPage(messagePageRequestVM.getPageIndex(),messagePageRequestVM.getPageSize());
        PageHelper.orderBy("create_time desc");
        List<Message> messageList = messageMapper.selectPage(messagePageRequestVM.getSendUserName(), messagePageRequestVM.getSubjectId(), messagePageRequestVM.getPublishContent());
        PageInfo<Message> pageInfo=new PageInfo<>(messageList);
        return pageInfo;
    }
    @Override
    public PageInfo<Message> selectPageForStudent(MessagePageRequestVM messagePageRequestVM) {
        PageHelper.startPage(messagePageRequestVM.getPageIndex(),messagePageRequestVM.getPageSize());
        PageHelper.orderBy("create_time desc");
        List<Message> messageList = messageMapper.selectPageForStudent(messagePageRequestVM.getSendUserName(), messagePageRequestVM.getSubjectId(), messagePageRequestVM.getPublishContent());
        PageInfo<Message> pageInfo=new PageInfo<>(messageList);
        return pageInfo;
    }

    @Override
    public List<Message> findByPid(Integer pid) {
        return messageMapper.findByPid(pid);
    }
    @Override
    public List<Message> findByContidion(Message message) {
        return messageMapper.findByCondition(message);
    }

    @Override
    public Message findById(Integer id) {
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void insertReply(Message reply) {
        messageMapper.insertSelective(reply);
    }

    @Override
    public int updateMessage(Message message) {
        return messageMapper.updateByPrimaryKeySelective(message);
    }

    @Async("asyncServiceExecutor")
    @Override
    public void insertByAiReply(Integer messageId,String content,String subject,Integer userId) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("query",content);
            JsonNode reply = aiUtil.qiReplyQuestion(jsonObject.toString());
            Message messageAi = new Message();
            messageAi.setUserId(userId);
            messageAi.setSubjectId(String.valueOf(subject));
            messageAi.setTime(DateTimeUtil.dateFormat(new Date()));
            messageAi.setPublishContent(String.valueOf(reply.get("result")));
            messageAi.setClassify(1);
            messageAi.setBelongs(String.valueOf(messageId));
            messageAi.setStatus(2);
            messageMapper.insertSelective(messageAi);
        } catch (JSONException e) {
            log.error("调用ai答疑失败，{}",e);
        }
    }
}
